<template>
	<div class="node-wrap">
		<!-- 节点显示 start -->
		<div class="node-wrap-box" :class="[disabled ? 'node-wrap-box--disabled' : '', `node-wrap-box--${nodeConfig.local_status}`]">
			<div class="title" style="background: #2bb58b">
				<el-icon class="icon"><Clock /></el-icon>
				<span v-show="!isEditTitle" class="title_label" @click="editTitle('box_nodeTitle')"
					>{{ nodeConfig.nodeName }}<el-icon v-if="!disabled" class="edit-icon"><edit /></el-icon
				></span>
				<el-input
					v-show="isEditTitle"
					ref="box_nodeTitle"
					v-model="nodeConfig.nodeName"
					clearable
					size="small"
					@blur="saveTitle"
					@keyup.enter="saveTitle"
				></el-input>
				<el-icon v-if="!disabled" class="close" @click.stop="delNode()"><close /></el-icon>
			</div>
			<div class="content" @click="show">
				<span v-if="toText(nodeConfig)">{{ toText(nodeConfig) }}</span>
				<span v-else class="placeholder">请选择触发器规则</span>
			</div>
		</div>
		<!-- 节点显示 end -->

		<!-- 添加节点 start -->
		<add-node v-model="nodeConfig.childNode" :disabled="disabled"></add-node>
		<!-- 添加节点 end -->

		<!-- 打开侧边弹窗 start -->
		<el-drawer v-model="drawer" title="触发器设置" destroy-on-close append-to-body :size="600" class="aDrawer">
			<template #header>
				<div class="node-wrap-drawer__title">
					<label v-show="!isEditTitle" @click="editTitle('nodeTitle')"
						>{{ form.nodeName }}<el-icon class="node-wrap-drawer__title-edit"><edit /></el-icon
					></label>
					<el-input v-show="isEditTitle" ref="nodeTitle" v-model="form.nodeName" clearable @blur="saveTitle" @keyup.enter="saveTitle" class="w-40"></el-input>
					<el-input v-model="form.nodeKey" clearable class="w-40 pl-1.5" placeholder="请填写nodeKey"></el-input>
				</div>
			</template>
			<el-container>
				<el-main>
					<el-radio-group v-model="form.triggerType" style="margin-bottom: 15px">
						<el-radio label="1">立即执行</el-radio>
						<el-radio label="2">延迟执行</el-radio>
					</el-radio-group>
					<el-form label-position="top">
						<template v-if="form.triggerType === '2'">
							<el-form-item label="">
								<el-radio-group v-model="form.delayType">
									<el-radio-button v-for="v of delayProcessSelfOptions" :key="v.value" :label="v.value">{{ v.label }}</el-radio-button>
								</el-radio-group>
							</el-form-item>

							<el-form-item v-if="form.delayType === '1'">
								<el-input v-model="fixedDuration" style="max-width: 300px" type="number" min="0" class="input-with-select">
									<template #append>
										<el-select v-model="fixedDurationType" style="width: 115px">
											<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
										</el-select>
									</template> </el-input
								><span>后进入下一步</span>
							</el-form-item>

							<el-form-item v-if="form.delayType === '2'">
								<el-time-picker v-model="automaticComputed" placeholder="时间点" value-format="HH:mm:ss" />
								<span>后进入下一步</span>
							</el-form-item>
						</template>
						<el-form-item label="">
							<el-input v-model="form.extendConfig.args" :rows="5" type="textarea" placeholder="请输入执行参数(json格式)" />
						</el-form-item>

						<el-form-item label="">
							<el-input
								v-model="form.extendConfig.trigger"
								placeholder="必填项，接口 TaskTrigger 实现
 class 如果不配置，调用全局实现子类"
							/>
						</el-form-item>
					</el-form>
				</el-main>
				<el-footer>
					<el-button type="primary" @click="save">保存</el-button>
					<el-button @click="drawer = false">取消</el-button>
				</el-footer>
			</el-container>
		</el-drawer>
		<!-- 打开侧边弹窗 end -->
	</div>
</template>

<script>
import addNode from './addNode'
import { delayProcessSelfOptions } from './config'
import { ElMessage } from 'element-plus'
const mapTip = {
	d: '天',
	h: '小时',
	m: '分钟'
}
export default {
	components: {
		addNode
	},
	props: {
		modelValue: { type: Object, default: () => {} },
		disabled: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			nodeConfig: {},
			drawer: false,
			isEditTitle: false,
			form: {},
			options: [
				{ label: '天', value: 'd' },
				{ label: '小时', value: 'h' },
				{ label: '分钟', value: 'm' }
			],
			// 固定时长 start
			fixedDuration: 1,
			fixedDurationType: 'm',
			selectedLabel: '分钟',
			// 固定时长 end

			// 自动计算 start
			automaticComputed: '',
			// 自动计算 end
			delayProcessSelfOptions
		}
	},
	watch: {
		modelValue() {
			this.nodeConfig = this.modelValue
		}
	},
	mounted() {
		this.nodeConfig = this.modelValue
		const { delayType, triggerType } = this.nodeConfig
		if (triggerType === '1') return
		const extendConfig = this.nodeConfig?.extendConfig?.time.split(':')
		if (delayType === '1') {
			this.fixedDuration = extendConfig[0]
			this.fixedDurationType = extendConfig[1]
		}
		if (delayType === '2') {
			this.automaticComputed = this.nodeConfig.extendConfig.time
		}
	},
	methods: {
		show() {
			if (this.disabled) return
			this.form = {}
			this.form = JSON.parse(JSON.stringify(this.nodeConfig))
			this.drawer = true
		},
		editTitle(refName) {
			if (this.disabled) return
			this.isEditTitle = true
			this.$nextTick(() => {
				if (this.$refs[refName]) {
					this.$refs[refName].focus()
				}
			})
		},
		saveTitle() {
			this.isEditTitle = false
		},
		save() {
			if (!this.form.nodeKey) {
				return ElMessage.error('请填写nodeKey')
			}
			if (this.form.triggerType === '2' && this.form.delayType === '1' && !Number(this.fixedDuration)) {
				return ElMessage.warning('等待时间数值最小为1')
			}
			this.form.extendConfig.time =
				this.form.triggerType === '2'
					? this.form.delayType === '1'
						? `${this.fixedDuration}:${this.fixedDurationType}`
						: `${this.automaticComputed}`
					: ''
			this.$emit('update:modelValue', this.form)
			this.drawer = false
		},
		delNode() {
			this.$emit('update:modelValue', this.nodeConfig.childNode)
		},
		toText(nodeConfig) {
			const { triggerType, delayType } = nodeConfig
			if (triggerType === '1') {
				return `立即执行`
			} else if (triggerType === '2') {
				if (delayType === '1') {
					return `延迟执行，等待${this.fixedDuration}${mapTip[this.fixedDurationType]}`
				} else if (delayType === '2') {
					const tip = `延迟执行，至当天`
					return !this.automaticComputed ? tip : `${tip}${this.automaticComputed}`
				}
			} else {
				return false
			}
		}
	}
}
</script>

<style></style>
